﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using Utils;
using ModbusTest.Config;
using System.Text.RegularExpressions;

namespace ModbusTest
{
     
    public class GasPressureTester
    {
        SerialPortUtil serialUtil;
        SerialPort _port;
        float TestValue;
        ModuleConfig _cfg;
        public event Action<string> ShowMsg;
        public GasPressureTester(SerialPort port,ModuleConfig cfg)
        {
            serialUtil = new SerialPortUtil(port);
            _cfg = cfg;
            _port = port;
            serialUtil.DataReceived += serialUtil_DataReceived;
            ShowMsg += GasPressureTester_ShowMsg;
        }

        void GasPressureTester_ShowMsg(string obj)
        {
            ;
        }

        void serialUtil_DataReceived(DataReceivedEventArgs e)
        {
                string data = e.DataReceived;
                ShowMsg(data);
                Regex rgx=new Regex(@"\d{3}\.\d{4}");
                Match m = rgx.Match(data);
                if (m.Success)
                {
                    float.TryParse(m.Value, out TestValue);
                    var s = from a in _cfg.Specifications where a.Description == "轴承孔直径" select a;
                    foreach (var b in s)
                    {
                        b.TestValue = TestValue;
                    }            
                }
        }
    }
}
